package com.java.simple.study.juc.pattern;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhoujl
 * @date 2022/5/11 20:46
 * @desc
 */
public class TestOrder {

    public static void main(String[] args) {
        //testFixOrderWaitNotify();
        testFixOrderParkUnpark();
    }

    // 用来同步锁的对象
    static Object obj = new Object();
    // 标识t2线程是否运行过
    static boolean t2runned = false;

    public static void testFixOrderWaitNotify() {
        new Thread(() -> {
            synchronized (obj) {
                while (!t2runned) {
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(1);
            }
        }, "t1").start();

        new Thread(() -> {
            System.out.println(2);
            synchronized (obj) {
                t2runned = true;
                obj.notifyAll();
            }
        }, "t2").start();

    }

    public static void testFixOrderParkUnpark() {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            System.out.println(1);
        }, "t1");
        t1.start();

        new Thread(() -> {
            System.out.println(2);
            LockSupport.unpark(t1);
        }, "t2").start();
    }


}

class SyncWaitNotify {
    private int flag;
    private int loopNumber;

    public SyncWaitNotify(int flag, int loopNumber) {
        this.flag = flag;
        this.loopNumber = loopNumber;
    }

    public void print(int waitFlag, int nextFlag, String str) {
        for (int i = 0; i < loopNumber; i++) {
            synchronized (this) {
                while (this.flag != waitFlag) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(str);
                this.flag = nextFlag;
                this.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        SyncWaitNotify syncWaitNotify = new SyncWaitNotify(1, 5);
        new Thread(() -> {
            syncWaitNotify.print(1, 2, "a");
        }).start();

        new Thread(() -> {
            syncWaitNotify.print(2, 3, "b");
        }).start();

        new Thread(() -> {
            syncWaitNotify.print(3, 1, "c");
        }).start();
    }

}

class AwaitSignal extends ReentrantLock {

    private int loopNumber;

    public AwaitSignal(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public void start(Condition first) {
        this.lock();
        try {
            first.signal();
        } finally {
            this.unlock();
        }
    }

    public void print(String str, Condition curr, Condition next) {
        for (int i = 0; i < loopNumber; i++) {
            this.lock();
            try {
                curr.await();
                System.out.print(str);
                next.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                this.unlock();
            }
        }
    }

    public static void main(String[] args) {
        AwaitSignal awaitSignal = new AwaitSignal(5);
        Condition aWaitSet = awaitSignal.newCondition();
        Condition bWaitSet = awaitSignal.newCondition();
        Condition cWaitSet = awaitSignal.newCondition();

        new Thread(() -> {
            awaitSignal.print("a", aWaitSet, bWaitSet);
        }).start();

        new Thread(() -> {
            awaitSignal.print("b", bWaitSet, cWaitSet);
        }).start();

        new Thread(() -> {
            awaitSignal.print("c", cWaitSet, aWaitSet);
        }).start();

        awaitSignal.start(aWaitSet);
    }

}

class SyncPark {
    private final int loopNumber;
    private Thread[] threads;

    public SyncPark(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public void setThreads(Thread... threads) {
        this.threads = threads;
    }

    public void print(String str) {
        for (int i = 0; i < loopNumber; i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(nextThread());
        }
    }

    public void start() {
        for (Thread thread : threads) {
            thread.start();
        }
        LockSupport.unpark(threads[0]);
    }

    private Thread nextThread() {
        Thread thread = Thread.currentThread();
        int index = 0;
        // 获取当前线程下表
        for (int i = 0; i < threads.length; i++) {
            if (thread == threads[i]) {
                index = i;
                break;
            }
        }
        if (index < threads.length - 1) {
            return threads[index + 1];
        }
        return threads[0];
    }

    public static void main(String[] args) {
        SyncPark syncPark = new SyncPark(5);
        Thread t1 = new Thread(() -> {
            syncPark.print("a");
        });
        Thread t2 = new Thread(() -> {
            syncPark.print("b");
        });
        Thread t3 = new Thread(() -> {
            syncPark.print("c");
        });
        syncPark.setThreads(t1, t2, t3);
        syncPark.start();
    }

}



